﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

using FSM.Bll;
using FSM.IBll;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

using Util.QuartzNet;

namespace FSM.AutoClean
{
    public class MainService
    {
        public async Task Start()
        {
            try
            {
                await WriteLog("FSM.AutoClean 启动，监听开始");

                var services = new ServiceCollection();
                ConfigureServices(services);
                var serviceProvider = services.BuildServiceProvider();
                var preDeleteBll = serviceProvider.GetService<ITbFilePredeleteBll>();
                CleanJob.PreDelteBll = preDeleteBll;
                await QuartzHelper.Instance.AddJob<CleanJob>("AutoClean", "AutoClean", "", "0 0/1 * * * ? *");
            }
            catch (Exception ex)
            {
                await WriteLog(ex.Message);
                throw;
            }
            Console.ReadKey();
        }

        public async Task Stop()
        {
            await QuartzHelper.Instance.DeleteJob("AutoClean", "AutoClean");
        }

        public async Task Pause()
        {
            await QuartzHelper.Instance.PauseJob("AutoClean", "AutoClean");
        }

        public async Task Continue()
        {
            await QuartzHelper.Instance.StartJob("AutoClean", "AutoClean");
        }

        private static void ConfigureServices(IServiceCollection services)
        {
            var configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", true, true)
                .Build();
            DLRegister.Regist(services);
        }

        private async static Task WriteLog(string message)
        {
            await Task.FromResult(0);
            Console.WriteLine($"[Main] {DateTime.Now:yyyy-MM-dd HH:mm:ss}:{message}");
        }
    }
}
